package com.ljh.controller;

import com.ljh.feign.MenuFeign;
import com.ljh.pojo.Menu;
import com.ljh.pojo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Value("${server.port}")
    public String port;
    @Autowired
    private MenuFeign menuFeign;

    @GetMapping("/findAll")
    @ResponseBody
    public MenuVo findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        return menuFeign.findAll(page, limit);
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") long id){
        menuFeign.deleteById(id);
        return "redirect:/account/redirect/menu_manage";
    }
    @PostMapping("/add")
    public String add(Menu menu){
        menuFeign.save(menu);
        return "redirect:/account/redirect/menu_manage";
    }
    @GetMapping("/findById/{id}")
    public String findById(@PathVariable("id") long id, Model model){
        model.addAttribute("menu",menuFeign.findById(id));
        model.addAttribute("list",menuFeign.findTypes());
        return "menu_update";
    }
    @PostMapping("/update")
    public String update(Menu menu){
        menuFeign.update(menu);
        return "redirect:/account/redirect/menu_manage";
    }

}
